﻿using DLL.NetStandard.FlushRobot.MS.Entity.Param;

using Prism.Commands;
using Prism.Events;
using Prism.Ioc;
using Prism.Regions;

using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Windows.Controls;
using System.Windows.Threading;
using DLL.NET70.Infrastructure.Prism.MVVM;
using DLL.NET70.Infrastructure.Prism;
using DLL.NET60.MotionCtrl.Entitys;
using DLL.Net60.MotionCtrl.ControlBase;

namespace PrismModule.MultyStationRobot.ViewModels
{
    public class PUC_MS_WorkPointParamViewModel : CMVVMBase, IRegionMemberLifetime
    {
        #region variable

        private CAxisesSpeed speedParam;
        private string _btnBrushes;

        public string btnBrushes
        {
            get { return _btnBrushes; }
            set { SetProperty(ref _btnBrushes, value); }
        }

        private IMotionCtrlBase _nMotionCtrl;

        public IMotionCtrlBase MotionCtrl
        {
            get { return _nMotionCtrl; }
            set { SetProperty(ref _nMotionCtrl, value); }
        }

        //BtnContent
        private CMS_WorkPointParam _obj = new CMS_WorkPointParam();

        public CMS_WorkPointParam Obj
        {
            get { return _obj; }
            set { SetProperty(ref _obj, value); }
        }

        private string _BtnContent = "修改";

        public string BtnContent
        {
            get { return _BtnContent; }
            set { SetProperty(ref _BtnContent, value); }
        }

        //xpos
        private string _XPos = "0.000";

        public string XPos
        {
            get { return _XPos; }
            set { SetProperty(ref _XPos, value); }
        }

        //ypos
        private string _YPos = "0.000";

        public string YPos
        {
            get { return _YPos; }
            set { SetProperty(ref _YPos, value); }
        }

        private double _ZPos = 0;

        public double ZPos
        {
            get { return _ZPos; }
            set { SetProperty(ref _ZPos, value); }
        }

        private int _ZCount = 6;

        public int ZCount
        {
            get { return _ZCount; }
            set { SetProperty(ref _ZCount, value); }
        }

        //SelectStep
        private string _SelectStep0 = "0.000";

        public string SelectStep0
        {
            get { return _SelectStep0; }
            set { SetProperty(ref _SelectStep0, value); }
        }

        private string _CurrentStep0 = "0.1";

        public string CurrentStep0
        {
            get { return _CurrentStep0; }
            set { SetProperty(ref _CurrentStep0, value); }
        }

        private string _CurrentStep1 = "0.1";

        public string CurrentStep1
        {
            get { return _CurrentStep1; }
            set { SetProperty(ref _CurrentStep1, value); }
        }

        private bool _Checked = false;

        public bool Checked
        {
            get { return _Checked; }
            set { SetProperty(ref _Checked, value); }
        }

        //Checked
        //CheckClickCommand
        public DelegateCommand CheckClickCommand { get; private set; }

        //btnclick
        public DelegateCommand<object> BtnClickCommand
        {
            get { return new DelegateCommand<object>(BtnClick); }
        }

        public DelegateCommand<object> MoveBtnClickCommand
        {
            get { return new DelegateCommand<object>(MoveBtnClick); }
        }

        public DelegateCommand ChangeSpeed0Command
        {
            get { return new DelegateCommand(ChangeSpeed0); }
        }

        public DelegateCommand ChangeSpeed1Command
        {
            get { return new DelegateCommand(ChangeSpeed1); }
        }

        public DelegateCommand ZMoveCommand
        {
            get { return new DelegateCommand(ZMove); }
        }

        private void CheckClick()
        {
            if (Checked == false)
            {
                BtnContent = "修改";
            }
            else
            {
                BtnContent = "复查";
            }
        }

        private void BtnClick(object button)
        {
            Button btn = (Button)button;
            string name = btn.Name;
            string content = name.Substring(3, 2);
            int id = Convert.ToInt32(content);
            int AxisID = 0;
            if (((id > 29) && (id < 38)) || ((id > 89) && (id < 95)))
            {
                AxisID = 0;
            }
            else
            {
                AxisID = 1;
            }
            double movePos = 0;
            switch (id)
            {
                case 30:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_StationPos0 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_StationPos0;
                        }
                        break;
                    }
                case 31:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_StationPos1 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_StationPos1;
                        }
                        break;
                    }
                case 32:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_StationPos2 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_StationPos2;
                        }
                        break;
                    }
                case 33:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_StationPos3 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_StationPos3;
                        }
                        break;
                    }
                case 34:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_StationPos4 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_StationPos4;
                        }
                        break;
                    }
                case 35:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_PutDrugsPos = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_PutDrugsPos;
                        }
                        break;
                    }
                case 36:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_PutDrugsPos4 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_PutDrugsPos4;
                        }
                        break;
                    }
                case 37:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_PutSolventPos = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_PutSolventPos;
                        }
                        break;
                    }
                case 90:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_CleanPos1 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_CleanPos1;
                        }
                        break;
                    }
                case 91:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_CleanPos2 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_CleanPos2;
                        }
                        break;
                    }
                case 92:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_CleanPos3 = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_CleanPos3;
                        }
                        break;
                    }
                case 93:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_YBStartPos = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_YBStartPos;
                        }
                        break;
                    }
                case 94:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.X_YBEndPos = Convert.ToDouble(XPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.X_YBEndPos;
                        }
                        break;
                    }
                //Y
                case 70:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_SafeHeight = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_SafeHeight;
                        }
                        break;
                    }
                case 71:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_SolventStartPunctureHeight = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_SolventStartPunctureHeight;
                        }
                        break;
                    }
                case 72:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_SolventHeight = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_SolventHeight;
                        }
                        break;
                    }
                case 73:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_BottleStartPunctureHeight = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_BottleStartPunctureHeight;
                        }
                        break;
                    }
                case 74:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_BottleEndPunctureHeight = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_BottleEndPunctureHeight;
                        }
                        break;
                    }
                case 75:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_FlushHeightLow = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_FlushHeightLow;
                        }
                        break;
                    }
                case 76:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_FlushHeightHight = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_FlushHeightHight;
                        }
                        break;
                    }
                case 77:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_PumpHeightHight = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_PumpHeightHight;
                        }
                        break;
                    }
                case 78:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_PumpHeightLow = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_PumpHeightLow;
                        }
                        break;
                    }
                case 79:
                    {
                        if (Checked == false)
                        {
                            //修改
                            Obj.Y_CleanPos2 = Convert.ToDouble(YPos);
                        }
                        else
                        {
                            //复查
                            movePos = Obj.Y_CleanPos2;
                        }
                        break;
                    }
                default:
                    break;
            }
            if (Checked == true)
            {
                //复查模式
                if (AxisID == 0)
                {
                    RecheckXPos(movePos);
                }
                else
                {
                    RecheckYPos(movePos);
                }
            }
        }

        private void RecheckXPos(double pos)
        {
            double xspeed = speedParam.axisesSpeed[0].JogFastSpeed;
            double yspeed = speedParam.axisesSpeed[1].JogFastSpeed;
            _nMotionCtrl.WaitForStop();
            _nMotionCtrl.AbsMove(1, yspeed, 0);
            _nMotionCtrl.WaitForStop();
            _nMotionCtrl.AbsMove(0, xspeed, pos);
            _nMotionCtrl.WaitForStop();
        }

        private void RecheckYPos(double pos)
        {
            double xspeed = speedParam.axisesSpeed[0].JogFastSpeed;
            double yspeed = speedParam.axisesSpeed[1].JogFastSpeed;
            _nMotionCtrl.WaitForStop();
            _nMotionCtrl.AbsMove(1, yspeed, pos);
            _nMotionCtrl.WaitForStop();
        }

        private void MoveBtnClick(object button)
        {
            Button btn = (Button)button;
            string name = btn.Name;
            string content = name.Substring(5, 1);
            int AxisID = 0;
            int RunDir = 1;
            if (Convert.ToInt32(content) == 1)
            {
                RunDir = 1;
            }
            else
            {
                RunDir = -1;
            }
            AxisID = Convert.ToInt32(name.Substring(3, 2));

            //_nMotionCtrl.SetCurrent(AxisID, (int)Current.Running, _nMotionCtrl.motionParam.axis.runParam.axisesRunParam[AxisID].JogCurrent);
            double ActSpeed = _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectJogSpeed;
            double ActStep = _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectRelDis;
            Message = "当前的JOG速度是" + ActSpeed + "mm/s" + "当前的步距是" + ActStep + "mm";
            _nMotionCtrl.sAxisID = AxisID;
            _nMotionCtrl.RunDir = RunDir;
            Thread thread = new Thread(new ThreadStart(_nMotionCtrl.JogStepRun));
            thread.IsBackground = true;
            thread.Start();
        }

        private void ZMove()
        {
            for (int i = 0; i < ZCount; i++)
            {
                _nMotionCtrl.AbsMove(i + 2, ZPos);
            }
            _nMotionCtrl.WaitForStop();
        }

        private void ChangeSpeed0()
        {
            ChangeStep(0);
        }

        private void ChangeSpeed1()
        {
            ChangeStep(1);
        }

        private int Axis0CurrentSelectedIndex = 0;
        private int Axis1CurrentSelectedIndex = 0;

        private void ChangeStep(int AxisID)
        {
            int CurrentSelectedIndex = 0;
            if (AxisID == 0)
            {
                CurrentSelectedIndex = Axis0CurrentSelectedIndex;
            }
            else
            {
                CurrentSelectedIndex = Axis1CurrentSelectedIndex;
            }
            if (CurrentSelectedIndex > 3)
            {
                CurrentSelectedIndex = 0;
            }
            switch (CurrentSelectedIndex)
            {
                case 0:
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectJogSpeed = speedParam.axisesSpeed[AxisID].JogSlowSpeed;
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectRelDis = speedParam.axisesSpeed[AxisID].RelDis1;
                    break;

                case 1:
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectJogSpeed = speedParam.axisesSpeed[AxisID].JogMiddleSpeed;
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectRelDis = speedParam.axisesSpeed[AxisID].RelDis2;
                    break;

                case 2:
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectJogSpeed = speedParam.axisesSpeed[AxisID].JogFastSpeed;
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectRelDis = speedParam.axisesSpeed[AxisID].RelDis3;
                    break;

                case 3:
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectJogSpeed = speedParam.axisesSpeed[AxisID].JogFastSpeed;
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectRelDis = speedParam.axisesSpeed[AxisID].RelDis4;
                    break;

                default:
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectJogSpeed = speedParam.axisesSpeed[AxisID].JogSlowSpeed;
                    _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectRelDis = speedParam.axisesSpeed[AxisID].RelDis3;
                    break;
            }
            double ActSpeed = _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectJogSpeed;
            double ActStep = _nMotionCtrl.motionParam.axis.speedParam.axisesSpeed[AxisID].SelectRelDis;
            Message = "当前的JOG速度是" + ActSpeed + "mm/s" + "当前的步距是" + ActStep + "mm";

            CurrentSelectedIndex = CurrentSelectedIndex + 1;
            if (AxisID == 0)
            {
                Axis0CurrentSelectedIndex = CurrentSelectedIndex;
                CurrentStep0 = ActStep.ToString();
            }
            else
            {
                Axis1CurrentSelectedIndex = CurrentSelectedIndex;
                CurrentStep1 = ActStep.ToString();
            }
            LogMessage(Message);
        }

        public DelegateCommand Home0Command
        { get { return new DelegateCommand(Home0); } }

        private void Home0()
        {
            _nMotionCtrl.Home(0);
        }

        public DelegateCommand Home1Command
        { get { return new DelegateCommand(Home1); } }

        private void Home1()
        {
            _nMotionCtrl.Home(1);
        }

        #endregion variable

        #region navigate

        public bool KeepAlive
        {
            get
            {
                //return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
                return false;//这个的作用是，只要离开当前切面，就注销掉当前页面
            }
        }

        public override void Loaded()
        {
            RegisterSaveCommand();
            _nMotionCtrl.ReadParam();
            l_Pos.Clear();
            for (int i = 0; i < _nMotionCtrl.motionParam.motionCardParam.UsedAxisCount; i++)
            {
                l_Pos.Add(0);
            }
            speedParam = _nMotionCtrl.motionParam.axis.speedParam;
            //设置调试电流
            for (int i = 0; i < _nMotionCtrl.motionParam.motionCardParam.UsedAxisCount; i++)
            {
                _nMotionCtrl.SetCurrent(i, (int)Current.Running, _nMotionCtrl.motionParam.axis.runParam.axisesRunParam[i].JogCurrent);
            }
            Axis0CurrentSelectedIndex = 0;
            Axis1CurrentSelectedIndex = 0;
            ChangeSpeed0();
            ChangeSpeed1();
            timer.Start();
            base.Loaded();
        }

        public override void UnLoaded()
        {
            UnRegisterSaveCommand();
            timer.Stop();
            base.UnLoaded();
        }

        public override void Save()
        {
            //获取当前页面的数据

            CMS_WR_WorkPointParam wrparam = new CMS_WR_WorkPointParam();
            wrparam.Param = Obj;
            wrparam.SaveParam();
            LogMessage("配药点位参数保存完成");
        }

        #endregion navigate

        private IContainerExtension _container;

        public PUC_MS_WorkPointParamViewModel(IRegionManager regionManager,
            IEventAggregator eventAggregator,
            IApplicationCommands applicationCommands,
            IMotionCtrlBase motionCtrlBase,
            IContainerExtension container) :
            base(regionManager, eventAggregator, applicationCommands)
        {
            _container = container;
            _nMotionCtrl = motionCtrlBase;
            CMS_WR_WorkPointParam wrparam = new CMS_WR_WorkPointParam();
            bool ret = wrparam.ReadParam();
            if (true == ret)
            {
                Obj = wrparam.Param;
            }
            Title = "配药点位";
            CheckClickCommand = new DelegateCommand(CheckClick);
            _nMotionCtrl.ReadParam();
            //添加定时器
            timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromMilliseconds(1000);
            timer.Tick += timer1_Tick;
        }

        private DispatcherTimer timer;

        private void timer1_Tick(object sender, EventArgs e)
        {
            //定时执行的内容
            UpdataPos();
        }

        private ObservableCollection<double> l_Pos = new ObservableCollection<double>();
        private ObservableCollection<int> l_Running = new ObservableCollection<int>();

        private void UpdataPos()
        {
            int axisCount = _nMotionCtrl.motionParam.motionCardParam.UsedAxisCount;
            long ret = 1;
            if (_nMotionCtrl != null)
            {
                if (_nMotionCtrl.CardConnected == 1)
                {
                    ret = _nMotionCtrl.GetPos();
                    if (0 == ret)
                    {
                        return;
                    }
                    else
                    {
                        l_Pos = _nMotionCtrl.AllAxisPos;
                    }
                    XPos = l_Pos[0].ToString("#0.00");//轴位置
                    YPos = l_Pos[1].ToString("#0.00");//轴位置

                    //读取传感器位置
                    GetAllInput();
                }
            }
        }

        private ObservableCollection<int> AllInput = new ObservableCollection<int>();

        private bool GetAllInput()
        {
            long ret = _nMotionCtrl.ReadInput();
            if (0 == ret)
            {
                return false;
            }
            AllInput = _nMotionCtrl.AllInput;
            if (_nMotionCtrl.motionParam.nIOSet.iPausePort > 0)
            {
                if (0 == AllInput[_nMotionCtrl.motionParam.nIOSet.iPausePort])
                {
                    btnBrushes = "Green";
                }
                else
                {
                    btnBrushes = "Gray";
                }
            }
            return true;
        }
    }
}